package ru.spbu.math.android.cbrf.dataprovider.handlers;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;
import ru.spbu.math.android.cbrf.model.Rate;
import ru.spbu.math.android.cbrf.model.StockItem;
import ru.spbu.math.android.cbrf.model.StockItemStore;

import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;

/**
 * @author Helga
 */
public class YandexSaxHandler extends DefaultHandler {
	
	private final String TAG = "Yandex Handler";

	private final String SDT = "sdt";
	private final String DATE = "date";
	private final String TIME = "time";
	private final String VALUE = "value";
	private final String STOCK = "stock";
	private final String ID = "id";

	private static String id = "";

	private List<Rate> rateList;
	/**
	 * @uml.property name="rate"
	 * @uml.associationEnd
	 */
	private Rate rate;
	private StringBuilder builder;

	private StockItem target;

	public void characters(char[] ch, int start, int length)
			throws SAXException {
		builder.append(ch, start, length);
	}

	public void endElement(String uri, String localName, String name)
            throws SAXException {
        if (this.rate != null) {
            if (localName.equalsIgnoreCase(VALUE)) {
            	try{
            	rate.setValue(Double.parseDouble(builder.toString()));
//            	Log.d(TAG, "Rate.setValue");
            	}catch (Exception e){
                String str = "";
                int length = builder.toString().length();
                int j =0;
                for(int k = length-1; k>0; k--){
                	if(Character.isDigit(builder.toString().charAt(k))||(Character.isDigit(builder.toString().charAt(k-1)))){
                		j = k;
                	}
                			
                }
                for(int i = j; i < length; i++){
                	str = str + builder.toString().charAt(i);
                }
                rate.setValue(Double.parseDouble(str));
 //               Log.d(TAG, "Rate.setValue modif");
            	}
            }else if (localName.equalsIgnoreCase(SDT)) {
                rateList.add(rate);
//                Log.d(TAG, "Add rate to rateList");
            }

            builder.setLength(0);
        }
    }

	public void startDocument() throws SAXException {
		rateList = new ArrayList<Rate>();
		builder = new StringBuilder();
//		Log.d(TAG, "Start document");
	}

	public void startElement(String uri, String localName, String name,
			Attributes attributes) throws SAXException {
		if (localName.equalsIgnoreCase(STOCK)) {
			id = attributes.getValue(ID);
//			Log.d(TAG, "Get ID");
		} else if (localName.equalsIgnoreCase(SDT)) {
			this.rate = new Rate();
//			Log.d(TAG, attributes.getValue(DATE) + " "
//					+ attributes.getValue(TIME));
			rate.setDate(attributes.getValue(DATE) + " "
					+ attributes.getValue(TIME), 1);
//			Log.d(TAG, "Set Date");
			StockItemStore itemStore = StockItemStore.Instance();
//			Log.d(TAG, "Instance");
			StockItem stockItem = new StockItem();
			stockItem = itemStore.findById(id);
			rate.setSource(stockItem);
			rate.setTarget(target);
//			Log.d(TAG, "Rate");
		}
	}

	public List<Rate> getRateItem() {
		return rateList;
	}

	public String getId(){
		return id;
	}

	public void setTarget(StockItem targetItem) {
		target = targetItem;

	}
}